package com.i2863.tools.weixinmp.util;

import java.net.MalformedURLException;
import java.net.URL;

public class UrlUtil {
	public static String getHostName(String url) {
		URL u = null;
		try {
			u = new URL(url);
			return u.getHost();
		} catch (MalformedURLException e) {
			return "";
		}
	}

	public static String getProtocol(String url) {
		URL u = null;
		try {
			u = new URL(url);
			return u.getProtocol();
		} catch (MalformedURLException e) {
			return "";
		}
	}

	public static String getPath(String url) {
		URL u = null;
		try {
			u = new URL(url);
			return u.getPath();
		} catch (MalformedURLException e) {
			return "";
		}
	}

	public static String getQuery(String url) {
		URL u = null;
		try {
			u = new URL(url);
			return u.getQuery();
		} catch (MalformedURLException e) {
			return "";
		}
	}

	public static boolean illegal(String url) {
		try {
			new URL(url);
			return true;
		} catch (MalformedURLException e) {
			return false;
		}

	}

	public static String joinUrl(String curl, String file) {

		try {
			URL url = new URL(new URL(curl), file);
			String q = url.toExternalForm();
			if (q.indexOf("#") != -1)
				q = q.replaceAll("^(.+?)#.*?$", "$1");
			return q;
		} catch (MalformedURLException e) {
			return null;
		}
	}

	public static void main(String[] args) {
		System.out.println(joinUrl("http://www.sccin.com.cn/InvestmentInfo/ZhaoBiao/InvitNotice.aspx?typeid=0&&type=ZBGG",
				"Template/Template_Standard_ZiGong.aspx?inviteNoticeID=128854#123"));
	}

}
